package com.sanyang.lawyer.controller.system;

import com.sanyang.lawyer.entity.*;
import com.sanyang.lawyer.service.hr.HrEmpService;
import com.sanyang.lawyer.service.system.AuthorityService;
import com.sanyang.lawyer.service.system.RoleService;
import com.sanyang.lawyer.service.system.UserService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: lawyer
 * @description: 权限控制类
 * @author: Mis.Feng
 * @create: 2019-10-11 10:10
 **/

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AuthorityService authorityService;
    @Autowired
    private HrEmpService empService;
    //登录页面
    @RequestMapping("login")
    public System_ResultBean login(String user_name, String user_pwd, HttpSession session){
        System.out.println(user_name+"---"+user_pwd);
        //测试spring框架封装的Md5
        String md5pass= DigestUtils.md5DigestAsHex(user_pwd.getBytes());
        System_user user=userService.login(user_name,md5pass);
        System.out.println(user);
        System_ResultBean resultBean=new System_ResultBean();
        if(user!=null&&!user.equals("")){
            resultBean.setCode(200);
            resultBean.setMsg("登陆成功");
            resultBean.setSessionid(session.getId());
            resultBean.setRedisName(user.getUserName());
            resultBean.setRedisId(user.getUserId());
            resultBean.setRole_id(user.getRoleId().getRoleId());
            user.setHrEmp(null);
            redisTemplate.opsForValue().set("user"+user.getUserId(),user);
            return resultBean;
        }else{
            resultBean.setCode(100);
            return resultBean;
        }
    }

    //判断旧密码
    @RequestMapping("getPwd")
    public String getPwd(String oldpwd,String userid){
        //测试spring框架封装的Md5
        String md5pass= DigestUtils.md5DigestAsHex(oldpwd.getBytes());
        String pwd=userService.getBypwd(Integer.parseInt(userid));
        if(pwd.equals(md5pass)){
            return "200";
        }
        return "100";
    }
    //修改密码
    @RequestMapping("updatePwd")
    public void updatePwd(String pwd,String userid){
        //测试spring框架封装的Md5
        String md5pass= DigestUtils.md5DigestAsHex(pwd.getBytes());
        userService.updatePwd(Integer.parseInt(userid),md5pass);
    }

    //根据角色id显示菜单栏
    @RequestMapping("role_authority")
    public List<System_authority> role_authority(HttpServletRequest request){
        String roleid=request.getParameter("role_id");
        String name=request.getParameter("redisName");
        List<System_authority> list=authorityService.getRoleAu(Integer.parseInt(roleid));
        return list;
    }

    //遍历用户表显示
    @RequestMapping("userlist")
    public Map<String,Object> userlist(HttpServletRequest request){
        Map<String,Object> map1=initMap(request);
        HrFlexigrid hrFlexigrid = new HrFlexigrid(map1,request);
        map1.put("Flexigrid",hrFlexigrid);
        List<System_user> list=userService.select(map1);
        System.out.println("11111");
        Integer count=userService.selectCount(map1);
        System.out.println(count);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);

        return map;
    }

    //遍历角色表显示
    @RequestMapping("rolelist")
    public Map<String,Object> rolelist(HttpServletRequest request){
        Map<String,Object> map1=initMap(request);
        HrFlexigrid hrFlexigrid = new HrFlexigrid(map1,request);
        map1.put("Flexigrid",hrFlexigrid);
        List<System_role> list=roleService.selectAll(map1);
        Map<String,Object> map=new HashMap<>();
        Integer  i=roleService.selectCount(map1);
        map.put("code",0);
        map.put("msg","");
        map.put("count",i);
        map.put("data",list);
        return map;
    }

    public Map<String,Object> initMap(HttpServletRequest request){
        String roleName=request.getParameter("roleName");
        String userName=request.getParameter("userName");
        String roleId=request.getParameter("roleId");
        Map map=new HashMap();
        map.put("roleName",roleName);
        map.put("userName",userName);
        map.put("roleId",roleId);
        return map;
    }
    @RequestMapping("upUser")
    public System_user upUser(String tableuserId){
        System_user user=userService.getById(Integer.parseInt(tableuserId));
        return user;
    }
    //用户表的所有角色
    @RequestMapping("listuserRole")
    public List<System_role> listuserRole(){
        return roleService.selectrole();
    }

    //下拉框中的所有角色
    @RequestMapping("listRole")
    public List<System_role> listRole(){
        return roleService.select();
    }

    //显示用户资料
    @RequestMapping("listAll")
    public System_user listAll(String userid){
        return userService.getById(Integer.parseInt(userid));
    }
    @RequestMapping("delUser")
    public void delUser(int userId){
        userService.delete(userId);
    }

    @RequestMapping("updateUser")
    public void updateUser(String userName,String roleName,String userid){
        userService.updateUser(Integer.parseInt(userid),userName,Integer.parseInt(roleName));
    }

    @RequestMapping("getByName")
    public List<Hr_emp> getByName(){
       List<Hr_emp> emp= empService.getByName();
       return emp;
    }

    @RequestMapping("insUser")
    public void insUser(String userName,String roleName,String hrName,String password){
        //测试spring框架封装的Md5
        String md5pass= DigestUtils.md5DigestAsHex(password.getBytes());
        userService.insert(userName,Integer.parseInt(roleName),Integer.parseInt(hrName),md5pass);
    }

    @RequestMapping("insUserEmp")
    public int  insUserEmp(String hrName){
        int i=userService.getByEmpId(Integer.parseInt(hrName));
        if(i!=0){
            return 200;
        }else {
            return 100;
        }
    }
    @RequestMapping("insUserName")
    public int  insUserName(String userName){
        int i=userService.getByuserName(userName);
        System.out.println(i);
        if(i!=0){
            return 200;
        }else {
            return 100;
        }
    }
}
